import { observable, action } from 'mobx-miniprogram'

export const store = observable({
  token: '',
  refresh_token: '',
  activeTabBarIndex: 3, // 选中项的索引
  info: {}, // 用户信息
  isLikedArticle: [], // 是否给文章点赞

  // actions 方法，用来修改 store 中的数据

  // 更新文章点赞
  updateArticleLiked: action(function(val) {
    if (!this.isLikedArticle.includes(val)) {
      this.isLikedArticle.push(val)
      console.log(this.isLikedArticle);
    } else {
      console.log(this.isLikedArticle);
      this.isLikedArticle.pop(val)
    }
  }),

  // 存储用户信息
  updateInfo: action(function(info) {
    this.info = info
  }),

  updateToken: action(function(token) {
    this.token = token
  }),
  updateRefreshToken: action(function(token) {
    this.refresh_token = token
  }),

  // 切换选中项的索引
  updateActiveTabBarIndex: action(function(index) {
    this.activeTabBarIndex = index
  })
})